import fs from 'fs';
import path from 'path';
import currentEnv from './currentEnv';

const rootPath = path.resolve(__dirname, '../');

const replaceContent = (param) => {
  let fileContent = fs.readFileSync(param.path, 'utf8');
  let reg = new RegExp(param.source, 'g');
  let newContent = fileContent.replace(reg, param.dest);
  fs.writeFileSync(param.path, newContent, 'utf8');
};

//苏小福定制：礼品卡相关功能改为提货券
const replaceLpkToThq = () => {
  if (currentEnv.project === 'suxiaofu' && currentEnv.release === 'yes') {
    const pathList = [
      '/src/pages/mainPages/standardMe/index.jsx',
      '/src/giftCardSubPack/myGiftCard/index.config.js',
      '/src/giftCardSubPack/myGiftCard/index.jsx',
      '/src/giftCardSubPack/giftCardShop/index.config.js',
      '/src/giftCardSubPack/giftCardShop/index.jsx',
      '/src/giftCardSubPack/giftCardRecharge/index.config.js',
      '/src/giftCardSubPack/giftCardRecharge/index.jsx',
      '/src/giftCardSubPack/giftCardOrderList/index.config.js',
      '/src/giftCardSubPack/giftCardDetail/index.config.js',
      '/src/giftCardSubPack/giftCardDetail/index.jsx',
      '/src/giftCardSubPack/giftCardBind/index.config.js',
      '/src/giftCardSubPack/giftCardBind/index.jsx',
      '/src/giftCardSubPack/giftCardBuyResult/index.jsx',
      '/src/giftCardSubPack/giftCardSelect/index.config.js',
      '/src/giftCardSubPack/giftCardSelect/index.jsx',
      '/src/giftCardSubPack/addGiftCard/index.config.js',
      '/src/giftCardSubPack/addGiftCard/index.jsx',
      '/src/giftCardSubPack/bindGiftCard/index.config.js',
      '/src/giftCardSubPack/bindGiftCard/index.jsx',
      '/src/orderSubPack/confirmOrder/OrderPriceDetail/GiftCardView/index.jsx',
      '/src/orderSubPack/orderDetail/index.jsx',
      '/src/mainSubPack/shopPay/index.jsx',
      '/src/mainSubPack/shopPayResult/index.jsx',
      '/src/pages/mainPages/paymentCode/cardSelect/index.jsx',
    ];
    const ruleList = [
      { source: '礼品卡', dest: '提货券' },
      { source: '新卡', dest: '提货券' },
      { source: '卡', dest: '券' },
    ];
    const picList = [
      {
        path: rootPath + '/src/giftCardSubPack/myGiftCard/index.jsx',
        source: 'empty.png',
        dest: 'empty_thq.png',
      },
      {
        path: rootPath + '/src/giftCardSubPack/giftCardBind/index.jsx',
        source: '卡密',
        dest: '密码',
      },
    ];
    try {
      for (let p = 0; p < picList.length; p++) {
        replaceContent(picList[p]);
      }
      for (let p = 0; p < pathList.length; p++) {
        for (let r = 0; r < ruleList.length; r++) {
          replaceContent({ path: rootPath + pathList[p], ...ruleList[r] });
        }
      }
      console.log('替换礼品卡为提货券 -- ok');
    } catch (error) {
      console.log('替换礼品卡为提货券 -- fail', error);
    }
  }
};

replaceLpkToThq();
